package io.mybatis.example.springboot.controller;

import io.mybatis.common.core.DataResponse;
import io.mybatis.common.core.RowsResponse;

import io.mybatis.example.springboot.model.SysUser;
import io.mybatis.example.springboot.service.SysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * sys_user - 用户信息表
 *
 * @author mkj
 */
@RestController
@RequestMapping("sysUsers")
public class SysUserController {

  @Autowired
  private SysUserService sysUserService;

  @PostMapping
  public DataResponse<SysUserDO> save(@RequestBody SysUserDO sysUser) {
    return DataResponse.ok(sysUserService.save( sysUser));
  }

  @GetMapping
  public RowsResponse<SysUserDO> findList(@RequestBody SysUserDO sysUser) {
    return RowsResponse.ok(sysUserService.findList( sysUser));
  }

  @GetMapping(value = "/{id}")
  public DataResponse<SysUserDO> findById(@PathVariable("id") Long id) {
    return DataResponse.ok(sysUserService.findById(id));
  }

  @PutMapping(value = "/{id}")
  public DataResponse<SysUserDO> update(@PathVariable("id") Long id, @RequestBody SysUserDO sysUser) {
    sysUser.setId(id);
    return DataResponse.ok(sysUserService.update( sysUser));
  }

  @DeleteMapping(value = "/{id}")
  public DataResponse<Boolean> deleteById(@PathVariable("id") Long id) {
    return DataResponse.ok(sysUserService.deleteById(id) == 1);
  }

}
